{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Simulating Coin Flips [playground]\n",
    "The code below shows how you can use Python (and the `random` library) to simulate coin flips.\n",
    "\n",
    "### Instructions\n",
    "\n",
    "1. Read through the code below. What do you think you'll see when you run this code?\n",
    "\n",
    "2. Run the code. How does the output compare to what you expected?\n",
    "\n",
    "3. Run a few more times. Do you get the same result?\n",
    "\n",
    "4. How might you change the code to ensure that the \"percent heads\" is **consistently** close to 50%? Try modifying the code so that the output is consistently within 1% of 50% (HINT: you can do this by changing ONE of the numbers in the first few lines of code)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "In 10000000 trials there were 4998007 heads and 5001993 tails\n",
      "PERCENT HEADS: 49.98007 percent\n"
     ]
    }
   ],
   "source": [
    "import random as rd\n",
    "\n",
    "num_trials = 10000000\n",
    "heads = 0\n",
    "tails = 0\n",
    "p_heads = 0.5\n",
    "\n",
    "for i in range(num_trials):\n",
    "    random_number = rd.random()\n",
    "    if random_number < p_heads:\n",
    "        heads = heads + 1\n",
    "    else:\n",
    "        tails += 1\n",
    "\n",
    "print(\"In\", num_trials, \"trials there were\", heads, \"heads and\", tails, \"tails\")\n",
    "print(\"PERCENT HEADS:\", 100 * heads/num_trials, \"percent\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# TODO - modify ONE LINE of the above code so that \n",
    "#        running the code above consistently gives a \n",
    "#        \"percent heads\" that's between 49 - 51 percent"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
